package com.stedu.service.Impl;

import com.stedu.bean.GamingPlatform;
import com.stedu.exception.SteduException;
import com.stedu.mapper.GamesMapper;
import com.stedu.mapper.GamingPlatformMapper;
import com.stedu.service.GamingPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class GamingPlatformServiceImpl implements GamingPlatformService {
    @Autowired
    private GamingPlatformMapper gamingPlatformMapper;
    @Autowired
    private GamesMapper gamesMapper;
    @Override
    public boolean add(GamingPlatform gamingPlatform) throws SteduException {
        if (gamingPlatformMapper.selectByDname(gamingPlatform.getPlaname())!=null){
            throw new SteduException("该平台名称已存在，无法添加");
        }
        return gamingPlatformMapper.add(gamingPlatform)==1?true:false;
    }

    @Override
    public boolean del(Integer plaid) throws SteduException {
        if (gamingPlatformMapper.selectById(plaid)==null){
            throw new SteduException("该频台不存在，无法删除");
        }
        if (gamesMapper.selectByDid(plaid).size()>0){
            throw new SteduException("该平台存在游戏，无法删除");
        }
        return gamingPlatformMapper.del(plaid)==1?true:false;
    }

    @Override
    public boolean update(GamingPlatform gamingPlatform) throws SteduException {
        if (gamingPlatformMapper.selectById(gamingPlatform.getPlaid())==null){
            throw new SteduException("该平台不存在，无法删除");
        }
        return gamingPlatformMapper.upda(gamingPlatform)==1?true:false;
    }

    @Override
    public List<GamingPlatform> selectall()  {
        return gamingPlatformMapper.selectall();
    }

    @Override
    public GamingPlatform selectById(Integer plaid)  {
        return gamingPlatformMapper.selectById(plaid);
    }
}
